package com.ffcs.web.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: 格式化日期．
 * @copyright: Copyright (c) 2011 FFCS All Rights Reserved
 * @company: 北京福富软件有限公司福州分公司
 * @author: 吴书永 2011-1-19
 * @version 1.00.00
 * @history:
 * 
 */
public class DateUtils {

	/**
	 * 日期格式化.
	 * 
	 * @param date
	 *            需要格式化的日期
	 * @param hms
	 *            格式化中的时,分秒为固定的.
	 * @return
	 */
	public static String dateToFormatString(Date date, String hms) {
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String string = sdf.format(date);
		return string + " " + hms;
	}

	/**
	 * 格式化为长日期格式
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToFullString(Date date) {
		String format = "yyyy-MM-dd hh:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String string = sdf.format(date);
		return string;
	}

	public static String dateToString(Date date) {
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String string = sdf.format(date);
		return string;
	}

	public static Date stringToDate(String dateString) {
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date fullStringToDate(String dateString) {
		String format = "yyyy-MM-dd hh:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将日期格式化成年月,用于10元全站产品.
	 */
	public static String getCheckMonth(Date date) {
		String format = "yyyyMM";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			String string = sdf.format(date);
			return string;
		} catch (Exception e) {
		}

		return null;
	}

	/**
	 * 格式化日期, 用于订单的交易流水号.
	 * 
	 * @param dateString
	 *            长格式的日期字符串. 2011-06-10 09:23:13
	 * @return
	 */
	public static String fullStringToDate2() {
		String dateString = "yyyyMMddHHmmss";
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(dateString);
		return df.format(date);
	}

	/**
	 * 获取当前时间串 默认格式 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrtDoneTimeString() {
		try {
			Date date = new Date();
			SimpleDateFormat dateFm = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			return dateFm.format(date);

		} catch (Exception e) {
			throw new RuntimeException("时间转换错误!", e);
		}

	}

	/**
	 * 获取当前时间串
	 * 
	 * @return
	 */
	public static String getCurrtDoneTimeString(String pattern) {
		try {
			Date date = new Date();
			SimpleDateFormat dateFm = new SimpleDateFormat(pattern);
			return dateFm.format(date);

		} catch (Exception e) {
			throw new RuntimeException("时间转换错误!", e);
		}

	}
}
